﻿using System.Collections.Generic;
using UnityEngine;

public enum ResolutionState
{
    RS_ORIGINAL = 0,
    RS_HIGH = 1,
    RS_LOW = 2
}

public class AppMgr : IZUpdate, IBaseData
{
    private int particleCut = 2;
    public List<GameObject> sceneEffect;

    private string[] strSetting = { "drive", "music", "sound", "display", "platform", "init" };

    public AppMgr()
    {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        //整个App设置一次的
        Application.targetFrameRate = 30;
        Application.runInBackground = true;
        sceneEffect = new List<GameObject>();
        UpdateMgr.GetSingleton().AddUpdate(this);
    }
    
    public void AdjustParticle(Transform go)
    {
        if (GetDisplayMode() == EDisplayMode.DISPLAY_SMOOTH)
        {
            ParticleSystem[] pss = go.GetComponentsInChildren<ParticleSystem>();
            for (int idx = 0; idx < pss.Length; ++idx)
            {
                pss[idx].emissionRate /= particleCut;
                pss[idx].maxParticles /= particleCut;
                if (pss[idx].maxParticles == 0)
                {
                    pss[idx].maxParticles = 1;
                }
            }
        }
    }

    public void AdjustSceneEffect()
    {
        if (sceneEffect != null)
        {
            for (int idx = 0; idx < sceneEffect.Count; ++idx)
            {
                if (sceneEffect[idx] != null)
                {
                    bool show = GetDisplayMode() == EDisplayMode.DISPLAY_HD;
                    Utility.SetActive(sceneEffect[idx], show);
                }
            }
        }
    }

    public void AdjustAllParticles(bool high)
    {
        ParticleSystem[] pss = GameObject.FindObjectsOfType<ParticleSystem>();
        for (int idx = 0; idx < pss.Length; ++idx)
        {
            if (!high)
            {
                pss[idx].emissionRate /= particleCut;
                pss[idx].maxParticles /= particleCut;
                if (pss[idx].maxParticles == 0)
                {
                    pss[idx].maxParticles = 1;
                }
            }
            else
            {
                pss[idx].emissionRate *= particleCut;
                pss[idx].maxParticles *= particleCut;
                if (pss[idx].maxParticles == 0)
                {
                    pss[idx].maxParticles = 1;
                }
            }
        }
    }
    
    // 音乐
    public EMusicMode GetMusicMode()
    {
        return (EMusicMode)GetValueInt((int)ESetting.SETTING_MUSIC);
    }

    // 音效
    public ESoundMode GetSoundMode()
    {
        return (ESoundMode)GetValueInt((int)ESetting.SETTING_SOUND);
    }

    //驾驶方式
    public EDriveMode GetDriveMode()
    {
        return (EDriveMode)GetValueInt((int)ESetting.SETTING_DRIVE);
    }

    // 显示
    public EDisplayMode GetDisplayMode()
    {
        return (EDisplayMode)GetValueInt((int)ESetting.SETTING_DISPLAY);
    }

    public int GetValueInt(int setting)
    {
        return UserPrefsMgr.GetKeyValueInt(strSetting[setting]);
    }

    public void SetValueInt(int setting, int value)
    {
        UserPrefsMgr.SetKeyValueInt(strSetting[setting], value);
    }
    
    public void ZUpdate()
    {

    }

    public void Init()
    {
        ZMgr.SoundMgr.SetMute((GetMusicMode() == EMusicMode.MUSIC_MUTE), (int)SoundTypeID.TYPE_BG);
        ZMgr.SoundMgr.SetMute((GetSoundMode() == ESoundMode.SOUND_MUTE), (int)SoundTypeID.TYPE_2D);
        ZMgr.SoundMgr.SetMute((GetSoundMode() == ESoundMode.SOUND_MUTE), (int)SoundTypeID.TYPE_3D);
        AdjustAllParticles(GetDisplayMode() == EDisplayMode.DISPLAY_HD);

        if (GetValueInt((int)ESetting.SETTING_INIT) == 0)
        {
            SetValueInt((int)ESetting.SETTING_DRIVE, (int)EDriveMode.DRIVE_PIAOYI);
            SetValueInt((int)ESetting.SETTING_INIT, (int)ESetting.SETTING_MUSIC);
        }
    }
}